package study.springcloud.config.nacos.controller;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.springcloud.config.nacos.support.utils.AopTargetUtils;
import study.eggs.result.Result;
import study.eggs.result.Results;

import java.util.Map;

@Slf4j
@RestController
public class _3_RedisController {

    @Autowired
    private RedisConnectionFactory factory;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/redis/factory")
    public Result factory() {
        //获取target
        LettuceConnectionFactory myFactory = (LettuceConnectionFactory) AopTargetUtils.getTarget(factory);
        //
        String hostName = myFactory.getHostName();
        String password = myFactory.getPassword();
        int database = myFactory.getDatabase();
        //
        Map<String, Object> data = Maps.newHashMap();
        data.put("hostName", hostName);
        data.put("password", password);
        data.put("database", database);
        return Results.ok(data);
    }

    @RequestMapping("/redis/template")
    public Result template(String value) {
        RedisConnectionFactory myFactory = redisTemplate.getConnectionFactory();
        //获取target
        LettuceConnectionFactory myFactory2 = (LettuceConnectionFactory) AopTargetUtils.getTarget(myFactory);
        //
        String hostName = myFactory2.getHostName();
        String password = myFactory2.getPassword();
        int database = myFactory2.getDatabase();
        //
        Map<String, Object> data = Maps.newHashMap();
        data.put("hostName", hostName);
        data.put("password", password);
        data.put("database", database);
        return Results.ok(data);
    }
}
